package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.CarListPopwindowBinding;
import com.sgb.kjwl.databinding.ItemCarinfoListBinding;
import com.sgb.kjwl.model.entity.CarInfo;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

public class CarListPopWindow extends BasePopWindow implements View.OnClickListener {

    private CarListPopwindowBinding mBinding;
    //
    private CarListAdapter mAdapter;
    private List<CarInfo> mList;

    public CarListPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.car_list_popwindow;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mBinding = (CarListPopwindowBinding) holder.getmBinding();
        initRecycler();
        initEvent();
    }

    private void initRecycler() {
        mList = new ArrayList<>();
        mAdapter = new CarListAdapter(mList);
        mBinding.rvList.setLayoutManager(new LinearLayoutManager(mContext));
        mBinding.rvList.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, mContext.getResources().getDimension(R.dimen.dp0_5), mContext.getResources().getColor(R.color.color_line)));
        mAdapter.bindToRecyclerView(mBinding.rvList);

    }

    private void initEvent() {
        mBinding.setOnClick(this);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            mAdapter.setSelect(position);
        });
    }

    public void setData(List<CarInfo> list) {
        mList.clear();
        if (list != null) {
            mList.addAll(list);
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        if (v == mBinding.tvCancel) {
            dissMiss();
        } else if (v == mBinding.tvSure) {
            if (mAdapter.select<0){
                MToast.showToast(mContext,"请选择车辆");
                return;
            }
            onResultClick.result(mAdapter.getSelect(), null, null);
            dissMiss();
        }
    }
    private static class CarListAdapter extends BaseQuickAdapter<CarInfo, BaseViewHolder> {
        int select=-1;//默认不选中 2.1.0版本

        public void setSelect(int select) {
            this.select = select;
            notifyDataSetChanged();
        }

        public int getSelect() {
            return select;
        }

        public CarListAdapter(@Nullable @org.jetbrains.annotations.Nullable List<CarInfo> data) {
            super(R.layout.item_carinfo_list,data);
        }

        @Override
        protected void convert(BaseViewHolder helper, CarInfo item) {
            ItemCarinfoListBinding mBingding= DataBindingUtil.bind(helper.itemView);
            mBingding.setViewModel(item);
            if(select==helper.getLayoutPosition()){
                mBingding.rlBg.setSelected(true);
                mBingding.ivSelect.setSelected(true);
            }else{
                mBingding.rlBg.setSelected(false);
                mBingding.ivSelect.setSelected(false);
            }

        }
    }
}
